package com.vids.utils;

import com.vids.model.response.ResponseStatus;
import com.vids.model.response.ResponseStatusBatchModel;
import com.vids.model.response.ResponseStatusModel;
import com.vids.model.response.ResponseStatusObject;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BeanUtilss {


    /**
     * obj 转 map
     * 
     * @param obj
     * @return
     * @throws Exception
     */
    public static Map<String, Object> objectToMap(Object obj) throws Exception {
	if (obj == null) {
	    return null;
	}
	Map<String, Object> map = new HashMap<String, Object>();

	Field[] declaredFields = obj.getClass().getDeclaredFields();
	for (Field field : declaredFields) {
	    field.setAccessible(true);
	    map.put(field.getName(), field.get(obj));
	}
	return map;
    }

    public static ResponseStatusBatchModel getBatchResponseModel(List<ResponseStatus> resps) {
	ResponseStatusBatchModel model = new ResponseStatusBatchModel();
	ResponseStatusObject responseStatusObject = new ResponseStatusObject();
	responseStatusObject.setResponseStatusObject(resps);
	model.setResponseStatusListObject(responseStatusObject);
	return model;
    }

    public static ResponseStatusModel getResponseModel(ResponseStatus status) {
	ResponseStatusModel model = new ResponseStatusModel();
	model.setResponseStatusObject(status);
	return model;
    }

    /**
     * 忽略大小写并转换bean类型除String/boolean/Integer类型外其他忽略
     *
     * @param orginObject
     *            转换的源对象
     * @param targetClass
     *            目标对象
     * @return 转换后的对象
     */
    public static <T> T transferObjectCommon(Object orginObject, Class targetClass) {
        T result = null;
        try {
            if (orginObject != null && !orginObject.equals("")) {
                result = (T) targetClass.newInstance();
                // 获取目标类的属性集合
                Map<String, Field> destPropertyMap = new HashMap<>();
                for (Field curField : targetClass.getDeclaredFields()) {
                    destPropertyMap.put(curField.getName().toLowerCase(), curField);
                }
                // 获取父类中的属性集合 只考虑一级继承
                for (Field curField : targetClass.getSuperclass().getDeclaredFields()) {
                    destPropertyMap.put(curField.getName().toLowerCase(), curField);
                }
                // 拷贝属性
                for (Field curField : orginObject.getClass().getDeclaredFields()) {
                    if (curField.getName().equalsIgnoreCase("serialVersionUID")) {
                        continue;
                    }
                    Class<?> type = curField.getType();
                    if (type == String.class || type == Integer.class || type == int.class || type == Date.class
                            || type == Double.class || type == boolean.class || type == Boolean.class) {
                        Field targetField = destPropertyMap.get(curField.getName().toLowerCase());
                        if (targetField != null) {
                            targetField.setAccessible(true);
                            curField.setAccessible(true);
                            targetField.set(result, curField.get(orginObject));
                        }
                    }
                }
                // 拷贝父类属性 只考虑一级继承
                for (Field curField : orginObject.getClass().getSuperclass().getDeclaredFields()) {
                    if (curField.getName().equalsIgnoreCase("serialVersionUID")) {
                        continue;
                    }
                    Class<?> type = curField.getType();
                    if (type == String.class || type == Integer.class || type == Double.class || type == Date.class
                            || type == Boolean.class || type == boolean.class || type == double.class
                            || type == int.class) {
                        Field targetField = destPropertyMap.get(curField.getName().toLowerCase());
                        if (targetField != null) {
                            targetField.setAccessible(true);
                            curField.setAccessible(true);
                            targetField.set(result, curField.get(orginObject));
                        }
                    }
                }
            }
        } catch (Exception e1) {
            e1.printStackTrace();
            return null;
        }
        return result;
    }
    /**
     * 忽略大小写转换bean类型
     *
     * @param obj
     *            转换的源对象
     * @param clz
     *            目标对象
     * @return 转换后的对象
     */
    public static <T> T transferObjectIgnoreCase(Object obj, Class clz) {
        T result = null;
        try {
            if (obj != null && !obj.equals("")) {
                result = (T) clz.newInstance();
                // 获取目标类的属性集合
                Map<String, Field> destPropertyMap = new HashMap<>();
                for (Field curField : clz.getDeclaredFields()) {
                    destPropertyMap.put(curField.getName().toLowerCase(), curField);
                }
                // 获取父类中的属性集合 只考虑一级继承
                for (Field curField : clz.getSuperclass().getDeclaredFields()) {
                    destPropertyMap.put(curField.getName().toLowerCase(), curField);
                }
                // 拷贝属性
                for (Field curField : obj.getClass().getDeclaredFields()) {
                    if (curField.getName().equalsIgnoreCase("serialVersionUID")) {
                        continue;
                    }
                    Field targetField = destPropertyMap.get(curField.getName().toLowerCase());
                    if (targetField != null) {
                        targetField.setAccessible(true);
                        curField.setAccessible(true);
                        targetField.set(result, curField.get(obj));
                    }
                }
                // 拷贝父类属性 只考虑一级继承
                for (Field curField : obj.getClass().getSuperclass().getDeclaredFields()) {
                    if (curField.getName().equalsIgnoreCase("serialVersionUID")) {
                        continue;
                    }
                    Field targetField = destPropertyMap.get(curField.getName().toLowerCase());
                    if (targetField != null) {
                        targetField.setAccessible(true);
                        curField.setAccessible(true);
                        targetField.set(result, curField.get(obj));
                    }
                }
            }
        } catch (Exception e1) {
            e1.printStackTrace();
            return null;
        }
        return result;
    }
}
